Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Size: px
Start display at page:

Download "Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות"

Transcription

1 Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

2 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented programs Lecture 5 discusses the dynamic, run time behavior Both are important, and both should be understood before we begin further investigation of object-oriented programming

3 3 Same Ideas, Different Terms All OOP languages have the following concepts, although the terms they use may differ: Classes, object type, factory object Instances, objects Message passing, method lookup, member function invocation, method binding Methods, member function, method function Inheritance, subclassing

4 Online Compiler Allows practicing new languages without the need to install an IDE. Everything you write is sent to the site. Don t write secret information. Don t use the site for solving assignments. Mark the private box. Otherwise, code you write is published on the site.

5 5 Objects as Examples of Abstract Data Types We noted that object-oriented programming, and objects in particular, can be viewed from many perspectives We described the many levels of abstraction from which one could examine a program In this chapter, we wish to view objects as examples of abstract data types

6 6 Encapsulation Programming that makes use of data abstractions is a methodological approach to problem solving where information is consciously hidden in a small parts of a program In particular, the programmer develops a series of abstract data types, each of which can be viewed as having two faces: push pop top const limit=300; var currenttop : 0.. limit; values : array [ 1.. limit ] of integer; The outside, or service view, describes what an object does The inside, or implementation view, describes how it does it

7 7 Encapsulation and Instantiation Classes provide a number of very important capabilities: Encapsulation The purposeful hiding of information, thereby reducing the amount of details that need to be communicated among programmers A Service View The ability to characterize an object by the service it provides, without knowing how it performs its task Instantiation The ability to create multiple instances of an abstraction

8 8 Instances and Instance Variables We have been using the term instance to mean a representative, or example, of a class We will accordingly use the term instance variable to mean an internal variable maintained by an instance Other terms we will occasionally use are: data field, or data members Each instance has its own collection of instance variables These values should not be changed directly by clients, but rather should be changed only by methods associated with the class Counter Instance Variable Counter value= value=

9 9 Behavior and State An object can also be viewed as a combination of behavior and state: Behavior: The actions that an instance can perform in response to a request Implemented by methods State: The data that an object must maintain in order to successfully complete its behavior Stored in instance variables (also known as data members, or data fields)

10 10 Class Definitions We will use as a running example the class definition for a playing card abstraction, and show how this appears in several languages Languages we will consider include Java, C++, C#, Delphi Pascal, Apple Pascal, Ruby, Python, Eiffel, Objective-C and Smalltalk

11 11 Programing Languages

12 12 C A general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie for use with the Unix operating system Although C was designed for implementing system software, it is also widely used for developing portable application software C is one of the most popular programming languages of all time and there are very few computer architectures for which a C compiler does not exist C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C

13 13 Class Definition C++ class PlayingCard { public: enum Suits {Spade, Diamond, Club, Heart}; Suits suit () { return suitvalue; } int rank () { return rankvalue; } private: Suits suitvalue; int rankvalue; }; public class PlayingCard { Java public enum Suit { Spade, Diamond, Club, Heart }; public int suit () { return suitvalue; }; public int rank () { return rankvalue; }; private int suitvalue; private int rankvalue;} enum Suits {Spade, Diamond, Club, Heart}; class PlayingCard { public Suits suit () { return suitvalue;} public int rank () { return rankvalue; } private Suits suitvalue; private int rankvalue; } C#

14 14 A Typical Example, Class Definition in C++ class PlayingCard { C++ public: enum Suits {Spade, Diamond, Club, Heart}; Suits suit () { return suitvalue; } int rank () { return rankvalue; } private: Suits suitvalue; int rankvalue; }; Note syntax for methods, instance variables, and visibility modifiers

15 15 Visibility Modifiers The terms public and private are used to differentiate the internal and external aspects of a class: Public features can be seen and manipulated by anybody They are the external (interface or service) view Private features can be manipulated only within a class They are the internal (implementation) view Typically methods are public and data fields are private, but either can be placed in either category

16 16 A C# Class Definition enum Suits {Spade, Diamond, Club, Heart}; C# class PlayingCard { public Suits suit () { return suitvalue; } public int rank () { return rankvalue; } } private Suits suitvalue; private int rankvalue; C# class definitions have minor differences: no semicolon at end enum cannot be nested inside class, and visibility modifiers are applied to methods and data fields individually

17 17 Java Class Definition public class PlayingCard { public enum Suit { Spade, Diamond, Club, Heart }; public int suit () { return suitvalue; }; public int rank () { return rankvalue; }; private int suitvalue; private int rankvalue;} Java also applies visibility modifiers to each item individually. The enum types are much more powerful than their counterparts in other languages: The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields

18 18 Enumeration in Java: Example RED false Green true

19 19 Definition in Java 1.5- The int enum pattern Problems: Not typesafe - since a suit is just an int you can pass in any other int value where a suit is required, or add two suits together (which makes no sense) Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is. class PlayingCard { public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4; public int suit () { return suitvalue; } public int rank () { return rankvalue; } private int suitvalue; private int rankvalue; }

20 20 Static and Final Notice how symbolic constants are defined in Java: static means that all instance share the same value. One per class. Similar meaning in many languages final is Java specific, and means it will not be reassigned. (C++ has const keyword that is similar, although not exactly the same) public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4;

21 21 Pascal Pascal is an influential imperative and procedural programming language Designed in 1968/9 and published in 1970 by Niklaus Wirth As a small and efficient language intended to encourage good programming practices using structured programming and data structuring

22 22 Pascal Dialects We will consider two dialects of Pascal, both descended from the earlier language. Apple Object Pascal: Defined by Apple Computer, once widely used on the Macintosh, now much less commonly used Delphi Pascal: Defined by Borland on the PC, still fairly widely used on that platform. (Called Kylix on the Linux platform) Many similarities due to the common heritage, but some notable differences. We consider only the language aspects of Delphi, there are many other features related to its visual interface that we will not describe

23 23 Class Definition in Apple Object Pascal type Suits = (Heart, Club, Diamond, Spade); PlayingCard = object suit : Suits; rand : integer; end; No explicit visibility modifiers (We ll later see syntax for methods)

24 24 Delphi Pascal type Suits = (Heart, Club, Diamond, Spade); TPlayingCard = class (TObject) public constructor Create (r : integer; s : Suits); function suit : Suits; function rank : int; private suitvalue : Suits; rankvalue : integer; end; Slightly different syntax, must name parent class, has visibility modifiers and constructors (more on those later)

25 25 Smalltalk Smalltalk doesn't have a textual description for classes, but instead you define classes in a visual interface Revolutionary idea in 1980, but now Visual Basic and Delphi programmers are used to similar facilities

26 26 Methods In the next revision of our playing card abstraction we make the following changes: We add a method that will return the face color of the card, either red or black We add a data field to maintain whether the card is face up or face down, and methods both to test the state of this value and to flip the card

27 27 Methods Although syntax will differ depending upon language, all methods have the following: A name that will be matched to a message to determine when the method should be executed A signature, which is the combination of the name and argument types. Methods with the same name can be distinguished by different signatures A return type A body, which is the code that will be executed when the method is invoked in response to a message

28 28 An Example, from C# class PlayingCard { // constructor, initialize new playing card public PlayingCard (Suits is, int ir) { suit = is; rank = ir; faceup = true; } // operations on a playing card public boolean isfaceup () { return faceup; } public int rank () { return rankvalue; } public Suits suit () { return suitvalue; } public void setfaceup (boolean up) { faceup = up; } public void flip () { setfaceup(!faceup);} public Color color () { if ((suit() == Suits.Diamond) (suit() == Suits.Heart)) return Color.Red; return Color.Black; } } // private data values private Suits suitvalue; private int rankvalue; private boolean faceup;

29 29 Constructor class PlayingCard { // constructor, initialize new playing card public PlayingCard (Suits is, int ir) { suit = is; rank = ir; faceup = true; }... } A constructor is a method that is used to initialize a newly constructed object In C++, Java, C# and many other languages it has the same name as the class We will talk about constructors more in the next chapter

30 30 Accessor (or getter) Methods An accessor (or getter) is a method that simply returns an internal data value: class PlayingCard {... // operations on a playing card public int rank () { return rankvalue; } public Suits suit () { return suitvalue; }... private int rankvalue; }

31 31 Why Use an Accessor? There are many reasons why an accessor is preferable to providing direct access to a data field: You can make the data field read-only It provides better documentation that the data field is accessible It makes it easier to later change the access behavior (e.g., count number of accesses) Some conventions encourage the use of a name that begins with get, (as in getrank()), but this is not universally followed

32 32 Setters (or mutators) A setter (sometimes called a mutator method) is a method that is used to change the state of an object: class PlayingCard { } // operations on a playing card public void setfaceup (boolean up) { faceup = up; }... // private data values private boolean faceup; Mutators are less common than accessors, but reasons for using are similar

33 33 Order of Methods For the most part, languages don't care about the order that methods are declared. Here are some guidelines: List important topics first. Constructors are generally very important, list them first Put public features before private ones Break long lists into groups List items in alphabetical order to make it easier to search Remember that class definitions will often be read by people other than the original programmer Remember the reader, and make it easy for them

34 34 Constant Data Fields Some languages allow data fields to be declared as constant (const modifier in C++, final in Java, other languages have other conventions). Constant data fields can be declared as public, since they cannot be changed. class PlayingCard { // Java example... public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4; }

35 35 Separation of Definition and Implementation In some languages (such as C++ or Object Pascal) the definition of a method can be separated from its implementation. They may even be in different files: class PlayingCard { public:... Colors color () ;... }; PlayingCard::Colors PlayingCard::color ( ) { // return the face color of a playing card if ((suit == Diamond) (suit == Heart)) return Red; else return Black; } Notice need for fully-qualified names.

36 36 Considerations in Method Definitions In C++ you have a choice to define a method in the class interface, or separately in an implementation file. How do you decide? Readability Only put very small methods in the class definition, so that it is easier to read Semantics Methods defined in class interface may (at the discretion of the compiler) be expanded in-line Another reason for only defining very small methods this way.

37 37 Variations on Classes We will consider a few of the mostly languagespecific variations on the idea of a class: Methods without Implementations -- interfaces in Java Properties in Delphi and C# Nested classes in Java and C++ Class Data fields

38 38 Interfaces in Java An interface is like a class, but it provides no implementation. Later, another class can declare that it supports the interface, and it must then give an implementation. public interface Storing { void writeout (Stream s); void readfrom (Stream s); }; public class BitImage implements Storing { void writeout (Stream s) {... } void readfrom (Stream s) {... } }; We will have much more to say about interfaces later after we discuss inheritance

39 39 Properties A property is manipulated syntactically in the fashion of a data field, but operates internally like a method That is, a property can be read as an expression, or assigned to as a value: writeln ('rank is ', acard.rank); (* rank is property of card *) acard.rank= 5; (* changing the rank property *) However, in both cases the value assigned or set will be mediated by a function, rather than a simple data value Properties are a way to define getters and setters, but allow them to be used as if they were simple assignments and expressions

40 40 Properties in Delphi type TPlayingcard = class (TObject) public... property rank : Integer read rankvalue; property suit : Suits read suitvalue write suitvalue; end; private rankvalue : Integer; suitvalue : Suits; Here we have made rank read only, but allowed suit to be both read and written It is also possible to make a property write-only, although this is not very common

41 41 Properties in C# public class PlayingCard { public int rank { get { return rankvalue; } set { rankvalue = value; } }... private int rankvalue; } Omitting a set makes it read-only, omitting a get makes it write-only

42 42 Inner or Nested Classes Why Use Nested Classes? The nested class has a close conceptual relationship to its surrounding class It is a way of logically grouping classes that are only used in one place It increases encapsulation: It can lead to more readable and maintainable code

43 43 Inner or Nested Classes Some languages (C++ or Java) allow a class definition to be given inside another class definition Whether the inner class can access features of the outer class is different in different languages class LinkedList {... private class Link { // inner class public int value; public Link next; } }

44 44 Inner or Nested Classes Both Java and C++ allow the programmer to write one class definition inside of another Such a definition is termed an inner class in Java, and a nested class in C++ Despite the similar appearances, there is a major semantic difference between the two concepts An inner class in Java is linked to a specific instance of the surrounding class (the instance in which it was created), and is permitted access to data fields and methods in this object A nested class in C++ is simply a naming device, it restricts the visibility of features associated with the inner class, but otherwise the two are not related

45 45 Class Data Fields Idea is that all instances of a class can share a common data field Simple idea, but how to resolve the following paradox. All instances have the same behavior: Either they all initialize the common area, which seems bad, or Nobody initializes the common area, which is also bad Different languages use a variety of mechanisms to get around this

46 46 Class Data Fields Resolving this paradox requires moving outside of the simple class/method/instance paradigm Another mechanism, not the objects themselves, must take responsibility for the initialization of shared data If objects are automatically initialized to a special value (such as zero) by the memory manager, then every instance can test for this special value, and perform initialization if they are the first However, there are other (and better) techniques

47 47 Static modifier Class CountingClass { CountingClass () { count = count + 1; // increment count } private static int count; // shared by all } static { // static block count = 0; }

48 48 Static Modifiers In both C++ and Java shared data fields are created using the static modifier We have seen a use of this already in the creation of symbolic constants in Java In Java the initialization of a static data field is accomplished by a static block Which is executed when the class is loaded

49 49 Initialization of static data in C++ In C++ there are two different mechanisms: Data fields that are static (or const) and represented by primitive data types can be initialized in the class body, as we have seen already. Alternatively, a global initialization can be defined that is separate from the class: class CountingClass { public: CountingClass() { count++;... } private: static int count; }; // global initialization is separate from class int CountingClass::count = 0;

50 50 Chapter Summary In this chapter we have examined the static, or compile time features of classes: The syntax used for class definition The meaning of visibility modifiers (public and private) The syntax used for method definition Accessor or getter methods, and mutator or setter methods Variations on class themes Interfaces Properties Nested classes Class data fields

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented

More information

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented

More information

Messages, Instances and Initialization

Messages, Instances and Initialization Messages, Instances and Initialization עזאם מרעי מבוסס על השקפים של הקורס תיכון תוכנה מונחה עצמים http://www.cs.bgu.ac.il/~oosd132/ http://www.cs.bgu.ac.il/~oosd142/ 2 Dynamic Aspects of Classes In the

More information

Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע "י ד"ר גרא וייס

Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע י דר גרא וייס Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע "י ד"ר גרא וייס 2 Roadmap In this chapter we will examine how differences

More information

Benefits of Data Encapsulation. Internal and External Views. Lecture 4 : Data Abstraction. Data Encapsulation. Information Hiding

Benefits of Data Encapsulation. Internal and External Views. Lecture 4 : Data Abstraction. Data Encapsulation. Information Hiding Lecture 4 : Data Abstraction ti with C++ : class Data Abstraction Data Abstraction ti Creating classes with appropriate functionality, which h hide the unnecessary details Data Encapsulation Implementation

More information

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון Overriding עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap A method in a child class overrides a method in the parent class if it has the same name and type signature: Parent void method(int,float)

More information

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות עי דר גרא וייס Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס 2 Roadmap In this chapter we will start to investigate the

More information

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we will start to investigate the concepts of inheritance and substitution: The intuitive and practical

More information

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

PROGRAMMING III OOP. JAVA LANGUAGE COURSE COURSE 3 PROGRAMMING III OOP. JAVA LANGUAGE PREVIOUS COURSE CONTENT Classes Objects Object class Acess control specifier fields methods classes COUSE CONTENT Inheritance Abstract classes Interfaces instanceof

More information

Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we will investigate some of the implications of the principle of substitution in statically typed

More information

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap In this chapter we will examine how differences in static and dynamic features effect object-oriented programming

More information

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Object interconnections גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Object interconnections גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון Object interconnections גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we move up a level of abstraction, and consider collections of objects working together Our focus will

More information

Fundamental Concepts and Definitions

Fundamental Concepts and Definitions Fundamental Concepts and Definitions Identifier / Symbol / Name These terms are synonymous: they refer to the name given to a programming component. Classes, variables, functions, and methods are the most

More information

Multiple Inheritance עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Multiple Inheritance עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון Multiple Inheritance עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we will investigate some of the logical problems that can arise when a language allows a child class to

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

Overloading המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Overloading המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון Overloading עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap In this chapter we will investigate the idea of overloading: Overloading based on scopes Overloading based on type signatures Coercion,

More information

CPS 506 Comparative Programming Languages. Programming Language

CPS 506 Comparative Programming Languages. Programming Language CPS 506 Comparative Programming Languages Object-Oriented Oriented Programming Language Paradigm Introduction Topics Object-Oriented Programming Design Issues for Object-Oriented Oriented Languages Support

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

Multiple Inheritance לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Multiple Inheritance לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Multiple Inheritance לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap In this chapter we will investigate some of the logical problems that can arise when a language allows a child class

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Inheritance and Substitution (Budd chapter 8, 10)

Inheritance and Substitution (Budd chapter 8, 10) Inheritance and Substitution (Budd chapter 8, 10) 1 2 Plan The meaning of inheritance The syntax used to describe inheritance and overriding The idea of substitution of a child class for a parent The various

More information

2.4 Structuring programs

2.4 Structuring programs 2.4 Structuring programs While theoretically a program could be written as one big expression, in reality we want some structure so that l The programmer has it easier to read the program l A compiler

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search Goal Generic Programming and Inner classes First version of linear search Input was array of int More generic version of linear search Input was array of Comparable Can we write a still more generic version

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

Classes Classes 2 / 36

Classes Classes 2 / 36 Classes 1 / 36 Classes Classes 2 / 36 Anatomy of a Class By the end of next lecture, you ll understand everything in this class definition. package edu. gatech. cs1331. card ; import java. util. Arrays

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

Abstraction עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Abstraction עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון Abstraction עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Tools for Programming Complex Software Fundamentally, people use only a few simple tools to create, understand, or manage complex systems

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Programming Exercise 14: Inheritance and Polymorphism

Programming Exercise 14: Inheritance and Polymorphism Programming Exercise 14: Inheritance and Polymorphism Purpose: Gain experience in extending a base class and overriding some of its methods. Background readings from textbook: Liang, Sections 11.1-11.5.

More information

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes 1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist

More information

Classes Classes 2 / 35

Classes Classes 2 / 35 Classes 1 / 35 Classes Classes 2 / 35 Anatomy of a Class By the end of next lecture, you ll understand everything in this class definition. package edu. gatech. cs1331. card ; import java. util. Arrays

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the

More information

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Arrays Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Object Quick Primer A large subset of Java s features are for OOP Object- Oriented Programming We ll get to that

More information

Abstract Data Types and Encapsulation Concepts

Abstract Data Types and Encapsulation Concepts Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept of abstraction

More information

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE 341, Autumn 2015, Ruby Introduction Summary CSE 341, Autumn 2015, Ruby Introduction Summary Disclaimer: This lecture summary is not necessarily a complete substitute for atting class, reading the associated code, etc. It is designed to be a useful

More information

Implementing Subprograms

Implementing Subprograms Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

3 ADT Implementation in Java

3 ADT Implementation in Java Object-Oriented Design Lecture 3 CS 3500 Spring 2010 (Pucella) Tuesday, Jan 19, 2010 3 ADT Implementation in Java Last time, we defined an ADT via a signature and a specification. We noted that the job

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)

More information

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented Table of Contents L01 - Introduction L02 - Strings Some Examples Reserved Characters Operations Immutability Equality Wrappers and Primitives Boxing/Unboxing Boxing Unboxing Formatting L03 - Input and

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

The Java Programming Language

The Java Programming Language The Java Programming Language Slide by John Mitchell (http://www.stanford.edu/class/cs242/slides/) Outline Language Overview History and design goals Classes and Inheritance Object features Encapsulation

More information

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages 1 / 26 CSE 307: Principles of Programming Languages Names, Scopes, and Bindings R. Sekar 2 / 26 Topics Bindings 1. Bindings Bindings: Names and Attributes Names are a fundamental abstraction in languages

More information

EECS168 Exam 3 Review

EECS168 Exam 3 Review EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any

More information

JavaScript: Sort of a Big Deal,

JavaScript: Sort of a Big Deal, : Sort of a Big Deal, But Sort of Quirky... March 20, 2017 Lisp in C s Clothing (Crockford, 2001) Dynamically Typed: no static type annotations or type checks. C-Like Syntax: curly-braces, for, semicolons,

More information

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System? Page 1 of 21 Page 2 of 21 and identity. Objects are members of a class, and the attributes and behavior of an object are defined by the class definition. The Essence of Object Oriented Programming with

More information

Chief Reader Report on Student Responses:

Chief Reader Report on Student Responses: Chief Reader Report on Student Responses: 2017 AP Computer Science A Free-Response Questions Number of Students Scored 60,519 Number of Readers 308 Score Distribution Exam Score N %At Global Mean 3.15

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation Outline Problem: Create, read in and print out four sets of student grades Setting up the problem Breaking

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc. Chapter 13 Object Oriented Programming Contents 13.1 Prelude: Abstract Data Types 13.2 The Object Model 13.4 Java 13.1 Prelude: Abstract Data Types Imperative programming paradigm Algorithms + Data Structures

More information

Ticket Machine Project(s)

Ticket Machine Project(s) Ticket Machine Project(s) Understanding the basic contents of classes Produced by: Dr. Siobhán Drohan (based on Chapter 2, Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

BM214E Object Oriented Programming Lecture 4

BM214E Object Oriented Programming Lecture 4 BM214E Object Oriented Programming Lecture 4 Computer Numbers Integers (byte, short, int, long) whole numbers exact relatively limited in magnitude (~10 19 ) Floating Point (float, double) fractional often

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Java Overview An introduction to the Java Programming Language

Java Overview An introduction to the Java Programming Language Java Overview An introduction to the Java Programming Language Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhan Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and dataoriented programming) - Ada 95 (also supports procedural and dataoriented

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

Declarations and Access Control SCJP tips

Declarations and Access Control  SCJP tips Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for

More information

Java is an objet-oriented programming language providing features that support

Java is an objet-oriented programming language providing features that support Java Essentials CSCI 136: Spring 2018 Handout 2 February 2 Language Basics Java is an objet-oriented programming language providing features that support Data abstraction Code reuse Modular development

More information

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8 Epic Test Review 1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4 Write a line of code that outputs the phase Hello World to the console without creating a new line character. System.out.print(

More information

Concepts of Programming Languages

Concepts of Programming Languages Concepts of Programming Languages Lecture 10 - Object-Oriented Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑   ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构 孟小亮 Xiaoliang MENG, 答疑 EMAIL: 1920525866@QQ.COM ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language

More information

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++ No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)

More information

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming) C++ Programming: Introduction to C++ and OOP (Object Oriented Programming) 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Brief introduction to C++ OOP vs. Procedural

More information

Imperative Languages!

Imperative Languages! Imperative Languages! Java is an imperative object-oriented language. What is the difference in the organisation of a program in a procedural and an objectoriented language? 30 class BankAccount { private

More information

Abstract data types &

Abstract data types & Abstract Data Types & Object-Oriented Programming COS 301 - Programming Languages Chapters 11 & 12 in the book Slides are heavily based on Sebesta s slides for the chapters, with much left out! Abstract

More information

Abstract Data Types & Object-Oriented Programming

Abstract Data Types & Object-Oriented Programming Abstract Data Types & Object-Oriented Programming COS 301 - Programming Languages Chapters 11 & 12 in the book Slides are heavily based on Sebesta s slides for the chapters, with much left out! Abstract

More information

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013 CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP Dan Grossman Winter 2013 Ruby logistics Next two sections use the Ruby language http://www.ruby-lang.org/ Installation / basic usage

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information